﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
using Bee.Security;
using Bee.Util;
using Bee.BLL;

namespace Bee.Web
{
    public class AuthMvcDispatcher : MvcDispatcher
    {
        protected override void ActionExecuting(ActionExecutingArgs actionExcutingArgs)
        {
            string controllerName = actionExcutingArgs.ControllerName.ToLower();
            string actionName = actionExcutingArgs.ActionName.ToLower();

            if (AuthManager.Instance.LoginFlag)
            {
                bool validFlag = AuthManager.Instance.CheckGrant(actionExcutingArgs.ControllerName
                , actionExcutingArgs.ActionName, actionExcutingArgs.Data);
                if (!validFlag)
                {
                    actionExcutingArgs.Result = Bee.Core.ActionExecutingResult.Cancel;
                    actionExcutingArgs.Message = "无权限访问:/{0}/{1}.bee  data:{2}".FormatWith(actionExcutingArgs.ControllerName
                        , actionExcutingArgs.ActionName, actionExcutingArgs.Data);
                }

                OnlineUserManager.Instance.TickUserId(AuthManager.Instance.CurrentUser.Id);
            }
            else
            {

                if (!AccessWhiteListManager.Instance.Check(controllerName, actionName))
                {
                    actionExcutingArgs.Result = Bee.Core.ActionExecutingResult.ToLogin;
                    actionExcutingArgs.Message = "请登入";
                }

            }
        }
    }
}
